package com.hwj.easybuysys.web;



import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hwj.easybuysys.model.EbNews;
import com.hwj.easybuysys.model.EbProduct;
import com.hwj.easybuysys.model.EbProductCategory;
import com.hwj.easybuysys.service.EbNewsService;
import com.hwj.easybuysys.service.EbProductCategoryService;
import com.hwj.easybuysys.service.EbProductService;
import jdk.nashorn.internal.ir.RuntimeNode;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/index")
public class IndexController {

    @Autowired
    private EbProductService ebProductService;
    @Autowired
    private EbProductCategoryService ebProductCategoryService;

    @Autowired
    private EbNewsService ebNewsService;

    @RequestMapping("/front")
    public String index(@RequestParam(defaultValue = "1")int psize,
                        @RequestParam(defaultValue = "8")int pageSize,
                        @RequestParam(defaultValue = "1")int a,
                        @RequestParam(defaultValue = "9")int b,
                        Model model,
                        HttpServletRequest request){
//        System.out.println("访问前台首页");

        //T000 1.分页商品数据
        Page<EbProduct>productPage = ebProductService.list(psize,pageSize);//商品
        Page<EbNews> newsPage = ebNewsService.list(a,b);//新闻
        //T000 2.最新的新闻数据

        //T000 3.商品分类数据
        Map<String, List<EbProductCategory>> cateMap = ebProductCategoryService.selectBigAndSmallCate();
        //T000 4.将数据绑定到model
        model.addAttribute("productPage",productPage);//绑定分页商品数据
        model.addAttribute("newsPage",newsPage);//新闻
       // model.addAttribute("cateMap",cateMap);//绑定分类数据
        request.getSession().setAttribute("cateMap",cateMap);
        //T000 5.指定视图
        //templates/index。html
        return "index";
    }
}
